package com.ctit.jbpm.action;

import java.util.List;

import org.jbpm.graph.def.ProcessDefinition;
import com.ctit.jbpm.bo.*;
import com.ctit.jbpm.vo.*;

public class CtitJbpmAction {

	private ProcessBo processBo = new ProcessBoImpl(); 
  
	/**
	 * 创建工作流实例
	 * 
	 * @param procName
	 * @throws Exception
	 */
// public void createProcessInstance(String procName) throws Exception {
// LDAPUser user = this.getLoginUser().getLdapUser();
// processBo.createProcessInstance(user, procName);
// this.deleteTaskInfoMap();
// }
	

	
	/**
	 * 部署工作流
	 * 
	 * @param xml
	 * @param schema
	 * @throws Exception
	 */
	public void deployProcessDefinition(String xml, String schema) throws Exception{

		processBo.deployProcessDefinition(xml, schema);
	}
	
	/**
	 * 删除工作流
	 * 
	 * @param procId
	 * @throws Exception
	 */
	public void deleteProcessDefinition(long procId) throws Exception{
	
		processBo.deleteProcessDefinitionById(procId);
	}
	
	/**
	 * 根据工作流ID找出工作流的xml和schema信息
	 * 
	 * @param procId
	 * @return ProcessDefinitionInfo
	 * @throws Exception
	 */
	public ProcessDefinitionInfo findProcessDefinitionInfo(int procId) throws Exception{
		ProcessDefinitionInfo procDefInfo = processBo.findProcessDefinitionInfoByProcId(procId);
		if(procDefInfo.getXml() == null || procDefInfo.getSchema() == null){
			throw new Exception("找不到该工作流的相关信息，您需要把该工作流导入到数据库中。");
// Schema schema = new Schema();
// String path =
// "E:\\HRWorkspace\\conghua-workflow\\processes\\酒类零售许可证\\processdefinition.xml";
// File file = new File(path);
// FileInputStream fis = new FileInputStream(file);
// byte[] b = new byte[(int)file.length()];
// for(int i=0; i<b.length; i++){
// b[i] = (byte)fis.read();
// }
// String xml = new String(b, "UTF-8");
// procDefInfo.setXml(xml);
// procDefInfo.setSchema(schema.createSchema(path));
		}
		return procDefInfo;
	}
	
	/**
	 * 查询所有的工作流
	 * 
	 * @return List<ProcessDefinition>
	 * @throws Exception
	 */
	public List<ProcessDefinition> findAllProcessDefinition() throws Exception{
		return processBo.findAllProcessDefinition();
	}
	

	
	/**
	 * 查询指定的工作流的历史版本
	 * 
	 * @param procName
	 * @return List<ProcessDefinition>
	 * @throws Exception
	 */
	public List<ProcessDefinition> findProcessAllVersions(String procName) throws Exception{
		List<ProcessDefinition> procDefs = this.processBo.findProcessAllVersions(procName);
		return procDefs;
	}
	


	

	


	
	/*
	 * public void processExport(int[] procIds, String path) throws Exception{
	 * path = path.replaceAll("\\\\", "/"); path = path.substring(0,
	 * path.lastIndexOf("/") + 1); File file = new File(path);
	 * if(file.isDirectory()){ for(int i=0; i<procIds.length; i++){
	 * ProcessDefinitionInfo procDefInfo =
	 * processBo.findProcessDefinitionInfoByProcId(procIds[i]);
	 * ProcessDefinition procDef =
	 * processBo.findProcessDefinitionById(procIds[i]); String xmlName =
	 * procDef.getName() + ".xml"; String schemaName = procDef.getName() +
	 * ".schema.xml"; this.writeFile(path + xmlName, procDefInfo.getXml());
	 * this.writeFile(path + schemaName, procDefInfo.getSchema()); } } else {
	 * throw new Exception("保存文件的路径不正确！"); } }
	 */
	
	
	/*
	 * public void processAllExport(String path) throws Exception{ path =
	 * path.replaceAll("\\\\", "/") + "/"; File file = new File(path);
	 * if(file.isDirectory()){ List<ProcessDefinition> procDefs =
	 * processBo.findAllProcessDefinition(); for(ProcessDefinition procDef :
	 * procDefs){ ProcessDefinitionInfo procDefInfo =
	 * processBo.findProcessDefinitionInfoByProcId((int)procDef.getId()); String
	 * xmlName = procDef.getName() + ".xml"; String schemaName =
	 * procDef.getName() + ".schema.xml"; this.writeFile(path + xmlName,
	 * procDefInfo.getXml()); this.writeFile(path + schemaName,
	 * procDefInfo.getSchema()); } } else { throw new Exception("保存文件的路径不正确！"); } }
	 */
	

	
	
	
	private String getTypeOfFile(String fileName) throws Exception{
		int index = fileName.lastIndexOf(".");
		String type = fileName.substring(index+1, fileName.length());
		return type;
	}
	
}
